package table.node;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MenuNode extends BaseNode {
	public static MenuNode getRootNode(){
		MenuNode root = null;
		ApplicationContext context = new ClassPathXmlApplicationContext("table/root.xml");
		Map<?, ?> tablesmap = context.getBeansOfType(MenuNode.class);

		for(Object bean : tablesmap.values()) {
			MenuNode menu = (MenuNode)bean;
			if(menu != null){
				root = menu;
				root.setPath("/0");
				break;
			}
		}
		return root;
	}

	private String path;
	private String name;
	private List<MenuNode> subMenuNodes;
	public void setPath(String path) {
		this.path = path;
	}
	public String getPath() {
		return path;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}

	public void setSubMenuNodes(List<MenuNode> pSubMenuNodes) {
		this.subMenuNodes = new ArrayList<MenuNode>();
		try{
			if(pSubMenuNodes != null && pSubMenuNodes.size() > 0){
				for(int i=0; i < pSubMenuNodes.size(); i++){
					MenuNode mNode = pSubMenuNodes.get(i);
					mNode.setPath("/" + i);
					this.subMenuNodes.add(mNode);
				}
			}
		}catch(Exception e){
		}
	}
	public List<MenuNode> getSubMenuNodes() {
		return subMenuNodes;
	}
	
	public MenuNode getMenuNodeByPath(String path){
		
		return null;
	}
	
	public String traveler() {
		String result = "";
		try{
			result += "<div class='menuHeader' path='"+ this.getPath() +"'>";
			result += "<a href='"+ ""+this.getNodeValue() +"'>" + this.getName() + "</a>";
				result += "<font class='handle'>+</font>";
			List<MenuNode> subMenuList = this.getSubMenuNodes();
			if(subMenuList != null && subMenuList.size() > 0){
				result += "<ul class='subMenuBox' >";
				for(int i=0; i < subMenuList.size(); i++){
					result += "<li>" + subMenuList.get(i).traveler() + "</li>";
				}
				result += "</ul>";
			}
			result += "</div>";
		}catch(Exception e){
		}
		return result;
	}

}
